import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import cookie from 'js-cookie'
 
// 创建axios实例
const service = axios.create({
    baseURL: 'http://localhost:8222',
    timeout: 15000 // 请求超时时间
})
// http request 拦截器
service.interceptors.request.use(
    config => {
    //从cookie获取token值，把token放到header里面
    if (cookie.get('token')) {
        config.headers['token'] = cookie.get('token')
    }
    return config
},
  err => {
    return Promise.reject(err)
})
// http response 拦截器
service.interceptors.response.use(
    response => {
        if (response.data.code !== 20000) {
            //返回状态码是28004 弹出登录框
            if (response.data.code == 28004) {
                //调用全局监听事件，弹出登录框
                loginEvent.$emit('loginDialogEvent')
                Message({
                message: "离开了太长时间，请重新登录",
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject(response.data)
            }
            // Message({
            //     message: response.data.message,
            //     type: 'error',
            //     duration: 5 * 1000
            // })
            // return Promise.reject(response.data)
        } else {
            return response.data
        }
    },
    error => {
        return Promise.reject(error.response)
})
export default service